import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/el-form.vue"; //弹框form

import {
    // addHntFlexuralClosedForm
    saveRjClosedForm, //闭合保存
    getRjClosedByMiXingId, //混凝土详情

    getMixingPileAlertByEN, //获取审核流程


} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "fCompanyid",
        "fCompanyname",
        "modelType", "keyType"
    ],
    data() {
        return {
            activeNames: ["0"],
            loading: false,
            handNum: false, //闭合申请单
            firstNum: false, //一级审核
            secondNum: false, //二级审核
            // AduitUser:null,
            isFetched: false,
            visible: "",
            title: "闭合处理",
            isLock: false,
            btnStatus: [],
            positionObj: [],
            dialogHeight: null,
            activeIndex: "1",
            baseFormObj: formJs.baseFormObj, //基础信息
            baseFormData: {},
            closeFormObj: formJs.closeFormObj, //闭合
            closeFileurllist: [], //附件
            closeFormData: { //闭合 closeFormObj

                fId: "", // 申请单fid //为空判断为新增
                fCompanyid: "", // 施工标段id
                fParentid: "", // 对应的超标记录fid
                aduitStatus: "", // 保存状态
                userId: "", // 当前登录人id
                userName: "", // 当前登录人姓名
                fWarningcontent: "", // 预警内容
                fReason: "", // 问题原因
                fHandleway: "", // 处理方式
                fHandleresult: "", // 处理结果
                fHandledates: this.common.day(0), // 处理日期
                fHandleusername: "", // 处理人名称
                fHandlefile: "", // 附件
                recordfiles: [], // 删除文件id集合
                fSecondcheckfile: "",
                fFirstcheckfile: ""
            },
            FauditFormObj: formJs.Faudit, //一级审核
            FauditFileurllist: [], //附件
            FauditFormData: { //闭合 closeFormObj

                fId: "", // 申请单fid //为空判断为新增
                fCompanyid: "", // 施工标段id
                aduitStatus: "", // 保存状态
                fParentid: "", // 对应的超标记录fid

                fFirstcheckresult: "", // 一级审核处理结果
                fFirstcheckopinion: "", // 一级审核处理意见
                fFirstcheckusername: "", // 一级审核处理人name
                fFirstcheckdates: this.common.day(0), // 一级审核处理日期

                userId: "", // 当前登录人id
                userName: "", // 当前登录人姓名
                recordfiles: [], // 删除文件id集合
            },
            SauditFormObj: formJs.Saudit, //二级审核
            SauditFileurllist: [],
            SauditFormData: { //闭合 closeFormObj

                fId: "", // 申请单fid //为空判断为新增
                fCompanyid: "", // 施工标段id
                aduitStatus: "", // 保存状态
                fParentid: "", // 对应的超标记录fid
                fSecondcheckresult: "", // 二级审核处理结果
                fSecondcheckopinion: "", // 二级审核处理意见
                fSecondcheckusername: "", // 二级审核处理人name
                fSecondcheckdates: this.common.day(0), // 二级审核处理日期
                userId: "", // 当前登录人id
                userName: "", // 当前登录人姓名
                recordfiles: [], // 删除文件id集合
            },
            btnShow: true,
            currentDetail: null,
            fCompanynames: null,
            fCompanyids: null,
            modelTypes: null,
            status: null,

        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },

        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        modelType: {
            handler(newVal) {
                if (newVal) {
                    this.modelTypes = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {

                    this.edit(newVal);
                }
            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                this.currentDetail = newVal;
            },
            deep: true,
            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {
                    // status:currentRow.fCheckstatus,companyId
                    this.status = newVal.status
                    this.fCompanyids = newVal.companyId

                }
            },
            immediate: true,
        },
        fCompanyname: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanynames = newVal

                }
            },
            immediate: true,
        },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
        this.btnShow = true
        this.getDetail({
            fId: this.currentDetail.fId
        }, this.currentDetail)
        if (this.statusNum == 1) { //闭合


        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.handNum = true; //闭合申请单
            this.firstNum = true; //一级审核
            this.secondNum = true; //二级审核
            // 闭合
            this.closeFormObj.map((item) => {
                item.disabled = true;
            });
            // 闭合
            // 一级审核
            this.FauditFormObj.map((item) => {
                item.disabled = true;
            });
            // 一级审核
            //  二级闭合
            this.SauditFormObj.map((item) => {
                item.disabled = true;
            });
            this.closeFormObj[3].uploadSelect = false
            this.FauditFormObj[4].uploadSelect = false
            this.SauditFormObj[4].uploadSelect = false
            //  二级闭合
        }
        this.getHeight();
    },
    methods: {
        // 折叠面板
        changeCollapse(e) {
            console.log(this.activeNames, e);
        },
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            if (b == "fManufacturername") { //生产厂家
                this.closeFormData.fManufacturername = data.label; //生产厂家
                this.closeFormData.fManufacturerid = data.value //生产厂家id
            } else if (b == "fSuppliername") { //供应商名称
                this.closeFormData.fSuppliername = data.label; //供应商
                this.closeFormData.fSupplierid = data.value //供应商id
            } else if (b == 'fSpecification') {
                this.closeFormData.fSpecification = data.value //供应商id
            }
        },
        edit() {
            this.closeFormData = this.$options.data.call(this).closeFormData;
        },
        // 刪除文件
        delteFile(file, status) {
            var obj;
            var arr;
            if (status == 0) {
                obj = this.closeFormData;
                arr = this.closeFileurllist;
            } else if (status == 1) {
                obj = this.FauditFormData;
                arr = this.FauditFileurllist;
            } else if (status == 2) {
                obj = this.SauditFormData;
                arr = this.SauditFileurllist;
            }
            for (let index = 0; index < arr.length; index++) {
                if (file.uid == arr[index].uid) {
                    if (arr[index].atta == undefined) {
                        obj.recordfiles.push({
                            fId: arr[index].fId,
                        });
                    } else {
                        obj.recordfiles.push({
                            fId: arr[index].atta.fId,
                        });
                    }
                    this.closeFileurllist.splice(index, 1);
                }
            }
        },
        getExamine(data) {
            console.log(data, 8888);
            var status = data.fCheckstatus;
            getMixingPileAlertByEN({
                    fCompanyid: data.fCompanyid,
                    fEquipmentnumber: data.equipmentNumber
                })
                .then((res) => {
                    if (res.errcode == 0) {
                        // 
                        var arr
                        if (data.fWarninglevel == "1") {
                            arr = res.data.data.fAduituser1.split("-");
                        } else if (data.fWarninglevel == "2") {
                            arr = res.data.data.fAduituser2.split("-");
                        } else if (data.fWarninglevel == "3") {
                            arr = res.data.data.fAduituser3.split("-");
                        }
                        console.log(this.userInfo.companyNature == arr[status]);
                        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
                        this.btnShow = true;
                        if ((this.userInfo.companyNature == arr[status])) {
                            if (status == 0 && data.fCloseoperation == '未闭合') {
                                // 闭合申请单 可编辑
                                this.handNum = true; //闭合申请单
                                this.firstNum = false; //一级审核
                                this.secondNum = false; //二级审核
                                this.activeNames = ["0"];
                                // 闭合
                                this.closeFormObj.map((item) => {
                                    item.disabled = true;
                                });
                                // this.closeFormData = this.$options.data.call(this).closeFormData;
                                this.closeFormObj[1].disabled = false;
                                this.closeFormObj[2].disabled = false;
                                this.closeFormObj[3].disabled = false;

                                this.closeFormObj[4].disabled = false;

                                // 闭合
                                // 一级审核
                                this.FauditFormObj.map((item) => {
                                    item.disabled = true;
                                });
                                // 一级审核
                                //  二级闭合
                                this.SauditFormObj.map((item) => {
                                    item.disabled = true;
                                });
                                //  二级闭合
                                this.closeFormObj[5].uploadSelect = true
                                this.FauditFormObj[4].uploadSelect = false
                                this.SauditFormObj[4].uploadSelect = false
                            } else if (status == 1 && data.fCloseoperation == '未闭合') {
                                // 一级审核
                                this.handNum = true; //闭合申请单
                                this.firstNum = true; //一级审核
                                this.secondNum = false; //二级审核

                                // 闭合
                                this.closeFormObj.map((item) => {
                                    item.disabled = true;
                                });

                                // 闭合
                                this.firstNum = true; //一级审核
                                // 一级审核
                                this.FauditFormObj.map((item) => {
                                    item.disabled = false;
                                });
                                this.FauditFormObj[2].disabled = true;
                                this.FauditFormObj[3].disabled = true;
                                // 一级审核
                                this.activeNames = ["0", "1"];


                                //  二级闭合
                                this.SauditFormObj.map((item) => {
                                    item.disabled = true;
                                });
                                this.closeFormObj[5].uploadSelect = false
                                this.FauditFormObj[4].uploadSelect = true
                                this.SauditFormObj[4].uploadSelect = false
                                //  二级闭合

                            } else if (status == 2 && data.fCloseoperation == '未闭合') {
                                //    二级审核
                                this.handNum = true; //闭合申请单
                                this.firstNum = true; //一级审核
                                this.secondNum = true; //二级审核

                                this.activeNames = ["0", "1", "2"];

                                // 闭合
                                this.closeFormObj.map((item) => {
                                    item.disabled = true;
                                });
                                // 闭合
                                // 一级审核
                                this.FauditFormObj.map((item) => {
                                    item.disabled = true;
                                });

                                // 一级审核
                                //  二级闭合
                                this.SauditFormObj.map((item) => {
                                    item.disabled = false;
                                });
                                this.SauditFormObj[2].disabled = true;
                                this.SauditFormObj[3].disabled = true;
                                this.closeFormObj[5].uploadSelect = false
                                this.FauditFormObj[4].uploadSelect = false
                                this.SauditFormObj[4].uploadSelect = true
                                //  二级闭合
                            } else if (status == 3 && data.fCloseoperation == '未闭合') {
                                this.btnShow = false

                                this.handNum = true; //闭合申请单
                                this.firstNum = true; //一级审核
                                this.secondNum = true; //二级审核
                                // 闭合
                                this.closeFormObj.map((item) => {
                                    item.disabled = true;
                                });
                                // 闭合
                                // 一级审核
                                this.FauditFormObj.map((item) => {
                                    item.disabled = true;
                                });
                                // 一级审核
                                //  二级闭合
                                this.SauditFormObj.map((item) => {
                                    item.disabled = true;
                                });
                                this.closeFormObj[5].uploadSelect = false
                                this.FauditFormObj[4].uploadSelect = false
                                this.SauditFormObj[4].uploadSelect = false
                                //  二级闭合
                            }
                        } else {
                            this.btnShow = false
                            // 闭合
                            this.closeFormObj.map((item) => {
                                item.disabled = true;
                            });
                            // 闭合
                            // 一级审核
                            this.FauditFormObj.map((item) => {
                                item.disabled = true;
                            });
                            // 一级审核
                            //  二级闭合
                            this.SauditFormObj.map((item) => {
                                item.disabled = true;
                            });
                            this.closeFormObj[5].uploadSelect = false
                            this.FauditFormObj[4].uploadSelect = false
                            this.SauditFormObj[4].uploadSelect = false
                            if (status == 0) {
                                this.handNum = true; //闭合申请单
                                this.firstNum = false; //一级审核
                                this.secondNum = false; //二级审核

                                //  二级闭合
                            } else if (status == 1) {
                                this.activeNames = ["0"]
                                this.handNum = true; //闭合申请单
                                this.firstNum = false; //一级审核
                                this.secondNum = false; //二级审核

                            } else if (status == 2) {
                                this.activeNames = ["0", "1"]

                                this.handNum = true; //闭合申请单
                                this.firstNum = true; //一级审核
                                this.secondNum = false; //二级审核

                            } else if (status == 3) {
                                this.activeNames = ["0", "1", "2"]

                                this.handNum = true; //闭合申请单
                                this.firstNum = true; //一级审核
                                this.secondNum = true; //二级审核

                            }
                        }

                        this.closeFormData.aduitStatus = status
                        this.FauditFormData.aduitStatus = status
                        this.SauditFormData.aduitStatus = status
                        // this.closeFormData.aduitStatus = (status - 0) + 1 + "" //按钮 
                        // this.FauditFormData.aduitStatus = (status - 0) + 1 + "" //按钮 
                        // this.SauditFormData.aduitStatus = (status - 0) + 1 + "" //按钮 
                    }
                })
                .catch((error) => {
                    console.log(error);
                });
        },
        // 详情
        getDetail(obj, newVal) {
            this.loading = true;
            getRjClosedByMiXingId(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.closeinfo

                    this.closeFormData.fParentid = this.currentDetail.fId //按钮 
                    this.FauditFormData.fParentid = this.currentDetail.fId //按钮 
                    this.SauditFormData.fParentid = this.currentDetail.fId //按钮 
                    this.closeFormData.fId = data.fId //按钮 
                    this.FauditFormData.fId = data.fId //按钮 
                    this.SauditFormData.fId = data.fId //按钮 
                    this.closeFormData.fNonresult = data.fNonresult
                    this.closeFormData.fHandlefile = data.fHandlefile; //  闭合
                    this.closeFormData.fFirstcheckfile = data.fFirstcheckfile; //  一级审核
                    this.closeFormData.fSecondcheckfile = data.fSecondcheckfile; //  一级审核

                    this.FauditFormData.fFirstcheckfile = data.fFirstcheckfile; //  一级审核
                    this.SauditFormData.fSecondcheckfile = data.fSecondcheckfile; //  一级审核

                    if (data.fHandledates == null || data.fHandledates == "") {
                        this.closeFormData.fHandledates = this.common.day(0)
                        this.closeFormData.fHandleusername = this.userInfo.realName; //处理人
                        this.closeFormData.fWarningcontent = data.fWarningcontent

                    } else {
                        this.closeFormData = data
                        this.closeFormData.recordfiles = []
                        this.closeFileurllist = res.data.fSourcefileList
                    }

                    // // 一级审核
                    if (data.fFirstcheckdates == null || data.fFirstcheckdates == "") {
                        this.FauditFormData.fFirstcheckdates = this.common.day(0)
                        this.FauditFormData.fFirstcheckusername = this.userInfo.realName; //一级审核人姓名
                    } else {
                        this.FauditFormData = data
                        this.FauditFormData.recordfiles = []
                        this.FauditFileurllist = res.data.fFirstSourcefileList
                    }
                    // // 二级审核
                    if (data.fSecondcheckdates == null || data.fSecondcheckdates == "") {
                        this.SauditFormData.fSecondcheckdates = this.common.day(0)
                        this.SauditFormData.fSecondcheckusername = this.userInfo.realName; //一级审核人姓名
                    } else {
                        this.SauditFormData = data

                        this.SauditFormData.recordfiles = []
                        this.SauditFileurllist = res.data.fSecondSourcefileList
                    }

                    this.getExamine(newVal)
                } else {
                    this.$message.error("列表数据获取失败", res.msg);
                    this.loading = false;
                }

            });
        },
        yj_saveForm() {
            this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));
            this.closeFormData.userId = this.userInfo.userId; //当前登录人id
            this.FauditFormData.userId = this.userInfo.userId; //当前登录人id
            this.FauditFormData.userName = this.userInfo.realName; //当前登录人名称
            // 二级审核
            this.SauditFormData.userId = this.userInfo.userId; //当前登录人id
            this.SauditFormData.userName = this.userInfo.realName; //当前登录人名称
            // 施工标段id
            this.SauditFormData.fCompanyid = this.fCompanyids
            this.FauditFormData.fCompanyid = this.fCompanyids
            this.closeFormData.fCompanyid = this.fCompanyids


            if (this.$refs.publicForm0.submitForm() && this.status == 0) {
                this.saveForm(this.closeFormData, 0);
            } else if (this.$refs.publicForm1.submitForm() && this.status == 1) {
                this.saveForm(this.FauditFormData, 1);
            } else if (this.$refs.publicForm2.submitForm() && this.status == 2) {
                this.saveForm(this.SauditFormData, 0);
            } else {
                console.log("error");
            }

        },
        // 保存
        saveForm(obj) {
            this.loading = true;

            this.isLock = true
            saveRjClosedForm(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;

                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.loading = false;

                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });
        },
    },
};